﻿@using Admin.Models
@{
    ViewBag.Title = "用户管理";
}


@( Html.Kendo().Grid<UserViewModel>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.UserName).Title("用户名").Filterable(f => f.Operators(o => o.ForString(d => d.Clear().IsEqualTo("等于").Contains("包含")))).Width(125);
                columns.Bound(p => p.Email).Title("邮箱").Filterable(f => f.Operators(o => o.ForString(d => d.Clear().IsEqualTo("等于").Contains("包含")))).Width(125);
                columns.Bound(p => p.RoleList).Title("角色").Filterable(false).ClientTemplate("#= getRoles(RoleList) #").Width(300);
                columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
            })
            .ToolBar(toolbar => toolbar.Create())
            .Editable(editable => editable.Mode(GridEditMode.InLine))
            .HtmlAttributes(new { style = "height: 550px;" })
            .Filterable()
            .Pageable()
            .Navigatable()
            .Sortable()
            .Scrollable(scr => scr.Height(550))
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Events(events => events.Error("error_handler"))
                .ServerOperation(false)
                .Model(model => { model.Id(p => p.Id);
                    model.Field(p => p.RoleList).DefaultValue(
                     ViewData["defaultRole"] as List<SelectItem>);
                })
                .Create(update => update.Action("User_Create", "UsersAdmin"))
                .Read(read => read.Action("Users_Read", "UsersAdmin"))
                .Update(update => update.Action("User_Update", "UsersAdmin"))
                .Destroy(update => update.Action("User_Destroy", "UsersAdmin"))
            )
)

<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors,
                function(key, value) {
                    if ('errors' in value) {
                        $.each(value.errors,
                            function() {
                                message += this + "\n";
                            });
                    }
                });
            alert(message);
        }
    }
    function getRoles(data) {
        var roles = "";
        for (var i = 0; i < data.length; i++) {
            roles = roles + data[i].Text+" ";
        }
        return roles;
    }



</script>

